home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 43.zip / Sources C- WorkDisk V.adf / ex / lines.c < prev    next >
C/C++ Source or Header  |  1987-02-15  |  6KB  |  309 lines

  1. /*
  2. *    A Simple program to display colorful lines on a custom screen.
  3. *    Compiles with no errors under Lattice 3.03, However, it passes ints to
  4. *    some subroutines and does structure assignments, so manx users will
  5. *    probably have to do a little work.
  6. *                        Paul Jatkowski
  7. *                        {ihnp4!}cuuxb!pej
  8. *
  9. *       Manxified by J. Van Houtven on Juli 16th 1987.
  10. *
  11. */
  12.  
  13. #include <exec/types.h>
  14. #include <stdio.h>
  15. #include <intuition/intuition.h>
  16. #include <graphics/gfxmacros.h>
  17. #include <graphics/view.h>
  18.  
  19. struct IntuitionBase *IntuitionBase;
  20. struct GfxBase *GfxBase;
  21. #define Q 5
  22. #define N (Q*12)    /* N is the number of lines displayed on the screen */
  23.             /* change the value of Q to change the number of    */
  24.             /* lines so that it is evenly divisible by the      */
  25.             /* number of color registers used            */
  26.  
  27. struct Pt
  28.        {
  29.     int x;
  30.     int y;
  31.        } from[N], to[N],dfrom, dto;
  32.  
  33.  
  34. int    curcolor;
  35. long    i,j;
  36. int    minx,maxx,miny,maxy;
  37.  
  38. extern int errno;
  39. char errmsg[50];
  40.  
  41. struct TextAttr MyFont = {
  42.    "topaz.font",           /* Font Name */
  43.    TOPAZ_SIXTY,            /* Font Height */
  44.    FS_NORMAL,              /* Style */
  45.    FPF_ROMFONT             /* Preferences */
  46. };
  47.  
  48.  
  49. struct NewScreen NewScreen = {
  50.    0,             /* left edge */
  51.    0,             /* top edge */
  52.    640,           /* Width (high res) */
  53.    400,           /* Height (interlace) */
  54.    4,             /* Depth */
  55.    0,1,           /* Detail and Block Pen Specification */
  56.    HIRES|LACE,      /* Hires Interlaced screen */
  57.    CUSTOMSCREEN,  /* The Screen Type */
  58.    &MyFont,       /* our font */
  59.    "My Own Screen", /* title */
  60.    NULL,          /* no special gadgets */
  61.    NULL           /* no special custom BitMap */
  62. };
  63.  
  64.  
  65. struct   Window   *Window;
  66. struct   Screen   *Screen;
  67. struct   RastPort  *RP;
  68.  
  69. main()
  70. {
  71.  
  72.     struct   NewWindow NewWindow;
  73.     struct   ViewPort  *VP;
  74.     int    notdone = 1;
  75.     struct    IntuiMessage *msg , *GetMsg();
  76.     int    my_rgbi,my_rgb[3];
  77.     int    inc,colorok;
  78.     int    lc = -1;
  79.  
  80.     /* open intuition library */
  81.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  82.     if (IntuitionBase == NULL)
  83.         exit(FALSE);
  84.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  85.     if (GfxBase == NULL)
  86.         exit (FALSE);
  87.  
  88.     if ((Screen = (struct Screen *)OpenScreen(&NewScreen)) == NULL)
  89.         exit(FALSE);
  90.     
  91.     /* set up new window structure */
  92.     NewWindow.LeftEdge = 0;
  93.     NewWindow.TopEdge = 0;
  94.     NewWindow.Width = 640;
  95.     NewWindow.Height = 400;
  96.     NewWindow.DetailPen = 0;
  97.     NewWindow.BlockPen = 1;
  98.     NewWindow.Title = "A Simple Window";
  99.     NewWindow.Flags = WINDOWCLOSE | SMART_REFRESH |
  100.                 WINDOWDRAG | WINDOWDEPTH | WINDOWSIZING;
  101.     NewWindow.IDCMPFlags = CLOSEWINDOW | NEWSIZE ;
  102.     NewWindow.Type = CUSTOMSCREEN;
  103.     NewWindow.FirstGadget = NULL;
  104.     NewWindow.CheckMark = NULL;
  105.     NewWindow.Screen = Screen;
  106.     NewWindow.BitMap = NULL;
  107.     NewWindow.MinWidth = 100;
  108.     NewWindow.MinHeight = 25;
  109.     NewWindow.MaxWidth = 640;
  110.     NewWindow.MaxHeight = 200;
  111.  
  112.     /* try to open the window */
  113.     if ((Window = (struct Window *)OpenWindow(&NewWindow)) == NULL)
  114.         exit(FALSE);
  115.    
  116.     RP = Window->RPort;
  117.     VP = (struct ViewPort *) ViewPortAddress(Window);
  118.    
  119.  
  120.     SetAPen(RP,14);
  121.     SetBPen(RP,0);
  122.     SetWindowTitles(Window,"Colorfull lines","");
  123.     SetDrMd(RP,JAM1);
  124.     SetAPen(RP,15);
  125.  
  126.     init();
  127.     SetRGB4(VP,0,0,0,0);     
  128.     SetRGB4(VP,1,1,3,4);     
  129.     SetRGB4(VP,2,5,0,8);  
  130.     SetRGB4(VP,3,0,8,5);     
  131.  
  132.     my_rgb[0] = rand() % 16;
  133.     my_rgb[1] = rand() % 16;
  134.     my_rgb[2] = rand() % 16;
  135.     SetRGB4(VP,curcolor,my_rgb[0],my_rgb[1],my_rgb[2]);
  136.     SetDrMd(RP,JAM1);            
  137.     while(notdone)
  138.     {
  139.         j = i;
  140.         if (++i >= N)
  141.             i = 0;
  142.  
  143.         SetAPen(RP,0);            /* erase old line */
  144.         Move(RP,from[i].x,from[i].y);
  145.         Draw(RP,to[i].x,to[i].y);
  146.  
  147.         from[i] = from[j];        /* structure assignment */
  148.         mv_point(&from[i], &dfrom);
  149.         to[i] = to[j];            /* structure assignment */
  150.         mv_point(&to[i], &dto);
  151.  
  152.         SetAPen(RP,curcolor);        /* draw a new line */
  153.         Move(RP,from[i].x,from[i].y);
  154.         Draw(RP,to[i].x,to[i].y);
  155.         
  156.         if ( (i % Q) == 0)
  157.         {
  158.             if (++curcolor > 15)
  159.                 curcolor = 4;
  160.             colorok = 0;
  161.             while (!colorok)
  162.             {
  163.                 inc = 1;
  164.                 if (rand() & 8 )
  165.                     inc = -1;
  166.                 my_rgbi = rand() % 3;
  167.                 inc += my_rgb[my_rgbi];
  168.                 /* make sure that the color register
  169.                    doesn't wrap and we don't change the same
  170.                    color twice in a row
  171.                 */
  172.                 if (inc <= 15 && inc >= 0 && lc != my_rgbi)
  173.                 {
  174.                     my_rgb[my_rgbi] = inc;
  175.                     colorok++;
  176.                     lc = my_rgbi;
  177.                 }
  178.             }
  179.             SetRGB4(VP,curcolor,my_rgb[0],my_rgb[1],my_rgb[2]);
  180.         }
  181.         if ( (rand() % 20) == 1)
  182.         {
  183.             if (rand() & 2)
  184.             {
  185.                 newdelta(&to[i],&dto);
  186.             }
  187.             else
  188.             {
  189.                 newdelta(&from[i],&dfrom);
  190.             }
  191.         }
  192.  
  193.         while ((msg = GetMsg(Window->UserPort)) != 0)
  194.         {
  195.             switch(msg->Class)
  196.             {
  197.  
  198.             case CLOSEWINDOW:    /* that's all folks */
  199.                 notdone = 0;
  200.                 ReplyMsg(msg);
  201.                 continue;
  202.             case NEWSIZE:
  203.                 ReplyMsg(msg);            
  204.                 init();
  205.                 break;            
  206.             default:
  207.                 ReplyMsg(msg);
  208.             }
  209.         }
  210.     }
  211.     
  212.  
  213.     /* close the window and  exit */
  214.     CloseWindow(Window);
  215.     CloseScreen(Screen);
  216.     exit(TRUE);
  217. }
  218.  
  219. die(s)
  220. char    *s;
  221. {
  222.         
  223.     SetWindowTitles(Window,"Fatal Error",s);
  224.     Wait(1<< Window->UserPort->mp_SigBit);
  225.     if (Window) CloseWindow(Window);
  226.     if (Screen) CloseScreen(Screen);
  227.     exit(0);
  228. }
  229.  
  230. init()
  231. {
  232.     ULONG    seconds,micros;
  233.     
  234.     miny = Window->BorderTop;
  235.     maxy = Window->Height - Window->BorderBottom;
  236.     minx = Window->BorderLeft;
  237.     maxx = Window->Width - Window->BorderRight;
  238.  
  239.     for (j=0 ; j <N ; j++)
  240.     {
  241.         from[j].x = 0; from[j].y = 0;
  242.         to[j].x = 0; to[j].y = 0;
  243.     }
  244.     /* attempt to ramdomize the random number generator */
  245.     CurrentTime(&seconds,µs);
  246.     srand(micros);
  247.     
  248.     from[0].x = range_rand(minx,maxx);
  249.     from[0].y = range_rand(miny,maxy);
  250.     from[1] = from[0];
  251.     
  252.     to[0].x = range_rand(minx,maxx);
  253.     to[0].y = range_rand(miny,maxy);
  254.     to[1] = to[0];
  255.  
  256.     newdelta(&from[0],&dfrom);
  257.     newdelta(&to[0],&dto);    
  258.     i = 0;
  259.     curcolor = 4;
  260.     
  261.     /* clear the screen */
  262.     SetAPen(RP,0);
  263.     SetOPen(RP,0);
  264.     RectFill(RP,minx,miny,maxx,maxy);
  265. }
  266.  
  267. range_rand(minv,maxv)
  268. {
  269.     register int i1;
  270.     
  271.     i1 = minv + (rand() % (maxv - minv));
  272. }
  273.  
  274. mv_point(p,dp)
  275. struct Pt *p, *dp;
  276. {
  277.     if ((p->x += dp->x) > maxx || p->x < minx)
  278.     {
  279.         dp->x = -dp->x;
  280.         p->x += dp->x;
  281.     }
  282.     if ((p->y += dp->y) > maxy || p->y < miny)
  283.     {
  284.         dp->y = -dp->y;
  285.         p->y += dp->y;
  286.     }
  287. }
  288. newdelta(p,dp)
  289. struct Pt *p, *dp;
  290. {
  291.     for (dp->x = getdelta() ;
  292.             ((p->x + dp->x) > maxx) || ((p->x + dp->x) < minx) ;
  293.             dp->x = getdelta())
  294.         ;
  295.     for (dp->y = getdelta() ;
  296.             ((p->y + dp->y) > maxy) || ((p->y + dp->y) < miny) ;
  297.             dp->y = getdelta() )
  298.         ;
  299. }
  300.  
  301. getdelta()
  302. {
  303.     register int x;
  304.  
  305.     x = (8 - (rand() % 16));
  306. }
  307.  
  308.  
  309.